﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySQLHomeWork
{
    class Order
    {
        [Key]
        public char OrderID { get; set; }
        [StringLength(32)]
        [Required]
        public string Client { get; set; }
        [Required]
        public double TotalPrice { get; set; }
        public List<orderDetails> Details { get; set; }
    }

    class orderDetails
    {
        [Key]
        public char ID { get; set; }
        [Required]
        public string ProductName { get; set; }
        [Required]
        public double Price { get; set; }
        [Required]
        public int Amount { get; set; }
        public int OrderID { get; set; }
    }

    class orderService : DbContext
    {
        public orderService() : base("name=connStr") 
        {
            
        }

        public DbSet<Order> orders { get; set; }

        public void addOrder(char orderID, string client, List<orderDetails> details)
        {
            using (var os = new orderService())
            {
                var ord = new Order();
                ord.OrderID = orderID;
                ord.Client = client;
                ord.Details = details;
                ord.TotalPrice = 0;
                foreach (orderDetails detail in details)
                    ord.TotalPrice += detail.Price * detail.Amount;
                os.orders.Add(ord);
                Console.WriteLine("已成功添加" + ord.OrderID);
                os.SaveChanges();
            }
        }

        public Order searchOrder(char orderID)
        {
            using (var os = new orderService())
            {
                var ord = os.orders.Find(orderID);
                return ord;
            }
        }
        public void deleteOrder(char orderID)
        {
            using (var os = new orderService())
            {
                Order ord = searchOrder(orderID);
                if (ord != null)
                    os.orders.Remove(ord);
                Console.WriteLine("已成功删除" + ord.OrderID);
                os.SaveChanges();
            }
        }
        public void changeOrder(char orderID)
        {
            using (var os = new orderService())
            {
                Order ord = searchOrder(orderID);
                if (ord != null)
                {
                    Console.WriteLine("修改client：");
                    ord.client = Console.ReadLine();
                    Console.WriteLine("删除detail：");
                    orderDetails det;
                    string v = Console.ReadLine();
                    foreach (orderDetails detail in ord.details)
                    {
                        if (detail.productName == v)
                        {
                            det = detail;
                            ord.details.Remove(det);
                            ord.totalPrice -= det.amount * det.price;
                        }
                    }
                    Console.WriteLine("添加detail：");
                    orderDetails det1 = new orderDetails();
                    det1.detailsID = orderID;
                    Console.WriteLine("输入productName：");
                    det1.productName = Console.ReadLine();
                    Console.WriteLine("输入amount：");
                    det1.amount = System.Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("输入price：");
                    det1.price = System.Convert.ToDouble(Console.ReadLine());
                    ord.details.Add(det1);
                    ord.totalPrice += det1.amount * det1.price;
                    os.SaveChanges();
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<orderDetails> details = new List<orderDetails>();
            orderDetails detail1 = new orderDetails();
            detail1.detailsID = '1';
            detail1.productName = "pen";
            detail1.price = 3.2;
            detail1.amount = 5;
            orderDetails detail2 = new orderDetails();
            detail2.detailsID = '1';
            detail2.productName = "book";
            detail2.price = 15.5;
            detail2.amount = 3;
            details.Add(detail1);
            details.Add(detail2);
            char orderID = '1';
            string client = "Bob";
            orderService os = new orderService();
            os.addOrder(orderID, client, details);
            os.searchOrder(orderID);
            os.deleteOrder(orderID);
            os.changeOrder(orderID);
        }
    }
}
